﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Abigail.Ui.Article
{
    public partial class UiEditArticleCategory : Form
    {
        Controller c;
        List<Model.ArticleCategory> items;
        bool isCreatingNew = false;
        int selectedMain;
        int currentId = 0;
        
        // Create new:
        public UiEditArticleCategory(Controller c, List<Model.ArticleCategory> items)
        {
            InitializeComponent();
            this.c = c;
            this.items = items;
            buildtree();
            tree_categories.ExpandAll();
            this.Show();
            isCreatingNew = true;
        }

        // Edit existing::
        public UiEditArticleCategory(Controller c, List<Model.ArticleCategory> items, int id, string name, int main)
        {
            InitializeComponent();
            this.c = c;
            this.items = items;
            buildtree();
            SetSelected(main);
            tree_categories.SelectedNode.Expand();
            txt_name.Text = name;
            selectedMain = main;
            currentId = id;

            this.Show();
            isCreatingNew = false;
        }

        private TreeNode FindNode(TreeNode givenNode, int id)
        {
            foreach (TreeNode node in givenNode.Nodes)
            {
                if (((Model.ArticleCategory)node.Tag).Id == id)
                {
                    return node;
                }
                else
                {
                    TreeNode nodeChild = FindNode(node, id);
                    if (nodeChild != null) return nodeChild;
                }
            }
            return (TreeNode)null;
        }

        private void SetSelected(int id)
        {
            if (id == 0)
            {
                tree_categories.SelectedNode = tree_categories.Nodes[0];
                return;
            }
            TreeNode node = tree_categories.Nodes[0];
            node = FindNode(node, id);
            tree_categories.SelectedNode = node;

        }

        private void buildtree()
        {
            tree_categories.Nodes.Clear();    // Clear any existing categories
            tree_categories.BeginUpdate();    // prevent overhead and flicker
            LoadBaseNodes();                  // load all the lowest tree nodes
            tree_categories.EndUpdate();      // re-enable the tree
            tree_categories.Refresh();        // refresh the treeview display
        }

        private void LoadBaseNodes()
        {
            TreeNode baseNode = tree_categories.Nodes.Add("Kategorier"); // add it to the tree
            baseNode.Tag = new Model.ArticleCategory();
            baseNode.Name = "top";

            int baseParent = 0;                 // Find the lowest root category parent value
            TreeNode node;
            foreach (Model.ArticleCategory cat in items)
            {
                if (cat.MainCategory < baseParent)
                    baseParent = cat.MainCategory;
            }
            foreach (Model.ArticleCategory cat in items)         // iterate through the categories
            {
                if (cat.MainCategory == baseParent)     // found a matching root item
                {
                    node = baseNode.Nodes.Add(cat.Name); // add it to the tree
                    node.Tag = cat;                 // send the category into the tag for future processing
                    getChildren(node);              // load all the children of this node
                }
            }
        }

        // recursive tree loader. Passes back in a node to retireve its childre
        // until there are no more children for this node.
        private void getChildren(TreeNode node)
        {
            TreeNode Node = null;
            Model.ArticleCategory nodeCat = (Model.ArticleCategory)node.Tag;  // get the category for this node
            foreach (Model.ArticleCategory cat in items)         // locate all children of this category
            {
                if (cat.MainCategory == nodeCat.Id)     // found a child
                {
                    Node = node.Nodes.Add(cat.Name);    // add the child
                    Node.Tag = cat;                         // set its tag to its category
                    getChildren(Node);                      // find this child's children
                }
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            c.DeleteArticleCategory(currentId, this);
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if (isCreatingNew)
                c.SaveNewArticleCategory(txt_name.Text, selectedMain, this);
            else
                c.UpdateArticleCategory(currentId, txt_name.Text, selectedMain, this);
        }
        
        private TreeNode FindBlueNode(TreeNode givenNode)
        {
            foreach (TreeNode node in givenNode.Nodes)
            {
                if (node.ForeColor == Color.Blue)
                {
                    return node;
                }
                else
                {
                    TreeNode nodeChild = FindBlueNode(node);
                    if (nodeChild != null) return nodeChild;
                }
            }
            return (TreeNode)null;
        }

        private void tree_categories_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = FindBlueNode(tree_categories.Nodes[0]);
            if (node != null)
                node.ForeColor = Color.Black;
            tree_categories.Nodes[0].ForeColor = Color.Black;

            ((TreeView)sender).SelectedNode.ForeColor = Color.Blue;

            if (tree_categories.SelectedNode != null)
            {
                
                if (tree_categories.SelectedNode.Name == "top")
                {
                    selectedMain = 0;
                    txt_main.Text = "Ingen";
                }
                else
                {
                    selectedMain = ((Model.ArticleCategory)tree_categories.SelectedNode.Tag).Id;
                    txt_main.Text = tree_categories.SelectedNode.Text;
                }
            }
        }
    }
}
